<!DOCTYPE html>
<html><head>
	<meta charset="UTF-8">
	<meta http-equiv="x-UA-Compatible" content="ie=Edge">
   	<meta name="viewport" content="width=device-width, initial-scale=1">
   	<title>Crafty - Crafty.mouseWheelDispatch</title>
	<link type="text/css" rel="stylesheet" href="../craftyjs-site.css">
	<link rel="shortcut icon" href="../favicon.ico">
</head>
<body>
	<div id="header-background"> </div>
	<div id="page-frame">
	<div id="header">
		<nav class="navbar">
  			<div class="container-fluid">
  				<div class="navbar-header">
  					<a class="navbar-brand" href="../"> <img class="logo" src="../images/text-logo.png"> </a>
  					
				</div>
				<div class="collapse navbar-collapse navbar-right" id="bs-example-navbar-collapse-1">
					<ul class="nav navbar-nav">
						<li><a href="../">首页</a></li>
						<li><a href="../getting-started/">快速开始</a></li>
						<li><a href="../documentation/">文档</a></li>
						<li><a href="../api/">API</a></li>
						<li><a href="../components/">组件</a></li>
						<li class="emph"><a href="../#install">下载</a></li>
					</ul>
				</div>
			</div>
		</nav>
	</div>


	<div id="main">
		<div id="content" class="container">
			



<div id="docs"><div class="toc-holder" id="doc-nav"><ul id="doc-level-one"><li><a href="events.html">事件列表</a></li><li class="category">核心<ul class="category-list"><li><a href="Crafty.html">Crafty</a></li><li><a href="Crafty Core.html">Crafty Core</a></li><li><a href="Crafty-bind.html">Crafty.bind()</a></li><li><a href="Crafty-c.html">Crafty.c()</a></li><li><a href="Crafty-clone.html">Crafty.clone()</a></li><li><a href="Crafty-defineField.html">Crafty.defineField()</a></li><li><a href="Crafty-e.html">Crafty.e()</a></li><li><a href="Crafty-extend.html">Crafty.extend()</a></li><li><a href="Crafty-frame.html">Crafty.frame()</a></li><li><a href="Crafty-getVersion.html">Crafty.getVersion()</a></li><li><a href="Crafty-init.html">Crafty.init()</a></li><li><a href="Crafty-isPaused.html">Crafty.isPaused()</a></li><li><a href="Crafty-one.html">Crafty.one()</a></li><li><a href="Crafty-pause.html">Crafty.pause()</a></li><li><a href="Crafty-s.html">Crafty.s()</a></li><li><a href="Crafty-settings.html">Crafty.settings</a></li><li><a href="Crafty-stage.html">Crafty.stage</a></li><li><a href="Crafty-stop.html">Crafty.stop()</a></li><li><a href="Crafty-support.html">Crafty.support</a></li><li><a href="Crafty-trigger.html">Crafty.trigger()</a></li><li><a href="Crafty-unbind.html">Crafty.unbind()</a></li><li><a href="Crafty-uniqueBind.html">Crafty.uniqueBind()</a></li></ul></li><li class="category">2D<ul class="category-list"><li><a href="2D.html">2D</a></li><li><a href="AngularMotion.html">AngularMotion</a></li><li><a href="Collision.html">Collision</a></li><li><a href="Crafty-circle.html">Crafty.circle</a></li><li><a href="Crafty-diamondIso.html">Crafty.diamondIso</a></li><li><a href="Crafty-HashMap.html">Crafty.HashMap</a></li><li><a href="Crafty-isometric.html">Crafty.isometric</a></li><li><a href="Crafty-map.html">Crafty.map</a></li><li><a href="Crafty-math-Matrix2D.html">Crafty.math.Matrix2D</a></li><li><a href="Crafty-math-Vector2D.html">Crafty.math.Vector2D</a></li><li><a href="Crafty-polygon.html">Crafty.polygon</a></li><li><a href="Crafty-raycast.html">Crafty.raycast()</a></li><li><a href="Crafty-rectManager.html">Crafty.rectManager</a></li><li><a href="Gravity.html">Gravity</a></li><li><a href="GroundAttacher.html">GroundAttacher</a></li><li><a href="Motion.html">Motion</a></li><li><a href="Supportable.html">Supportable</a></li></ul></li><li class="category">动画<ul class="category-list"><li><a href="Crafty-easing.html">Crafty.easing</a></li><li><a href="SpriteAnimation.html">SpriteAnimation</a></li><li><a href="Tween.html">Tween</a></li></ul></li><li class="category">资源<ul class="category-list"><li><a href="Crafty-asset.html">Crafty.asset()</a></li><li><a href="Crafty-assets.html">Crafty.assets</a></li><li><a href="Crafty-imageWhitelist.html">Crafty.imageWhitelist()</a></li><li><a href="Crafty-load.html">Crafty.load()</a></li><li><a href="Crafty-paths.html">Crafty.paths()</a></li><li><a href="Crafty-removeAssets.html">Crafty.removeAssets()</a></li></ul></li><li class="category">音频<ul class="category-list"><li><a href="Crafty-audio.html">Crafty.audio</a></li></ul></li><li class="category">控制器<ul class="category-list"><li><a href="Controllable.html">Controllable</a></li><li><a href="Controls.html">Controls</a></li><li><a href="Draggable.html">Draggable</a></li><li><a href="Fourway.html">Fourway</a></li><li><a href="Jumper.html">Jumper</a></li><li><a href="Multiway.html">Multiway</a></li><li><a href="Twoway.html">Twoway</a></li></ul></li><li class="category">调试<ul class="category-list"><li><a href="Crafty-error.html">Crafty.error()</a></li><li><a href="Crafty-log.html">Crafty.log()</a></li><li><a href="DebugCanvas.html">DebugCanvas</a></li><li><a href="DebugPolygon.html">DebugPolygon</a></li><li><a href="DebugRectangle.html">DebugRectangle</a></li><li><a href="SolidAreaMap.html">SolidAreaMap</a></li><li><a href="SolidHitBox.html">SolidHitBox</a></li><li><a href="VisibleMBR.html">VisibleMBR</a></li><li><a href="WiredAreaMap.html">WiredAreaMap</a></li><li><a href="WiredHitBox.html">WiredHitBox</a></li></ul></li><li class="category">事件<ul class="category-list"><li><a href="Crafty-addEvent.html">Crafty.addEvent()</a></li><li><a href="Crafty-bind.html">Crafty.bind()</a></li><li><a href="Crafty-one.html">Crafty.one()</a></li><li><a href="Crafty-removeEvent.html">Crafty.removeEvent()</a></li><li><a href="Crafty-trigger.html">Crafty.trigger()</a></li><li><a href="Crafty-unbind.html">Crafty.unbind()</a></li><li><a href="Crafty-uniqueBind.html">Crafty.uniqueBind()</a></li></ul></li><li class="category">游戏循环<ul class="category-list"><li><a href="Crafty-timer.html">Crafty.timer</a></li></ul></li><li class="category">图形<ul class="category-list"><li><a href="Canvas.html">Canvas</a></li><li><a href="CanvasLayer.html">CanvasLayer</a></li><li><a href="Color.html">Color</a></li><li><a href="Crafty-assignColor.html">Crafty.assignColor()</a></li><li><a href="Crafty-background.html">Crafty.background()</a></li><li><a href="Crafty-createLayer.html">Crafty.createLayer()</a></li><li><a href="Crafty-defaultShader.html">Crafty.defaultShader()</a></li><li><a href="Crafty-domHelper.html">Crafty.domHelper</a></li><li><a href="Crafty-pixelart.html">Crafty.pixelart()</a></li><li><a href="Crafty-sprite.html">Crafty.sprite()</a></li><li><a href="Crafty-WebGLShader.html">Crafty.WebGLShader()</a></li><li><a href="DOM.html">DOM</a></li><li><a href="DomLayer.html">DomLayer</a></li><li><a href="HTML.html">HTML</a></li><li><a href="Image.html">Image</a></li><li><a href="Particles.html">Particles</a></li><li><a href="Renderable.html">Renderable</a></li><li><a href="Sprite.html">Sprite</a></li><li><a href="Text.html">Text</a></li><li><a href="WebGL.html">WebGL</a></li><li><a href="WebGLLayer.html">WebGLLayer</a></li></ul></li><li class="category">输入<ul class="category-list"><li><a href="AreaMap.html">AreaMap</a></li><li><a href="Button.html">Button</a></li><li><a href="Crafty-findPointerEventTargetByComponent.html">Crafty.findPointerEventTargetByComponent()</a></li><li><a href="Crafty-keyboardDispatch.html">Crafty.keyboardDispatch()</a></li><li><a href="Crafty-keydown.html">Crafty.keydown</a></li><li><a href="Crafty-keys.html">Crafty.keys</a></li><li><a href="Crafty-lastEvent.html">Crafty.lastEvent</a></li><li><a href="Crafty-mouseButtons.html">Crafty.mouseButtons</a></li><li><a href="Crafty-mouseDispatch.html">Crafty.mouseDispatch()</a></li><li><a href="Crafty-mouseWheelDispatch.html">Crafty.mouseWheelDispatch()</a></li><li><a href="Crafty-multitouch.html">Crafty.multitouch()</a></li><li><a href="Crafty-selected.html">Crafty.selected</a></li><li><a href="Crafty-touchDispatch.html">Crafty.touchDispatch()</a></li><li><a href="Keyboard.html">Keyboard</a></li><li><a href="Mouse.html">Mouse</a></li><li><a href="MouseDrag.html">MouseDrag</a></li><li><a href="Touch.html">Touch</a></li></ul></li><li class="category">杂项<ul class="category-list"><li><a href="Crafty-addEvent.html">Crafty.addEvent()</a></li><li><a href="Crafty-device.html">Crafty.device</a></li><li><a href="Crafty-removeEvent.html">Crafty.removeEvent()</a></li><li><a href="Crafty-support.html">Crafty.support</a></li></ul></li><li class="category">模型<ul class="category-list"><li><a href="Model.html">Model</a></li></ul></li><li class="category">场景<ul class="category-list"><li><a href="Crafty-scene.html">Crafty.scene()</a></li></ul></li><li class="category">资源<ul class="category-list"><li><a href="Crafty-background.html">Crafty.background()</a></li><li><a href="Crafty-scene.html">Crafty.scene()</a></li><li><a href="Crafty-viewport.html">Crafty.viewport</a></li></ul></li><li class="category">工具<ul class="category-list"><li><a href="Crafty-math.html">Crafty.math</a></li><li><a href="Delay.html">Delay</a></li><li><a href="Storage.html">Storage</a></li></ul></li></ul></div><div id="doc-content" class="doc-page-holder"><div class="doc-page"><h1>Crafty.mouseWheelDispatch() <span></span></h1><div class="doclet" id="Crafty-mouseWheelDispatch"><span class="markdown"><p>Crafty 分派接收到的鼠标滚轮事件的内部方法。</p>
</span><div class="triggered-events"><h4>事件</h4><div class="trigger-list"><dl class="trigger"><dt>MouseWheelScroll <span>[<span class="trigger-data"><span class="obj-name">Scroll direction (up | down)</span><span class="obj-props"> = { direction: +1 | -1}</span></span>]</span></dt><dd>当鼠标在舞台上滚动时触发</dd></dl></div></div><span class="markdown"><p>这个方法处理被 <code>Crafty.stage.elem</code> 接收的本地 <code>mousewheel</code> 事件（所有浏览器，除了火狐）或者 <code>DOMMouseScroll</code> 事件（只有火狐）。事件参数将会增加一个 <code>.direction</code> 属性，并分派到全局的 Crafty 对象和每一个实体。</p>
<p>注意，滚轮的步长属性在每一个浏览器都不一样，因为建议用检查 <code>.direction</code> 代替。<code>.direction</code> 等于 <code>+1</code> 表示滚轮向上滚动，<code>-1</code> 表示向下滚动。（见 <a href="http://stackoverflow.com/questions/5527601/normalizing-mousewheel-speed-across-browsers">详情</a>）。</p>
</span><div class="example"><h4>例子</h4><span class="markdown"><p>在鼠标滚轮事件中缩放视口（摄像机）：</p>
<pre><code class="hljs ">Crafty.bind(<span class="hljs-string">"MouseWheelScroll"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">evt</span>) </span>{
    Crafty.viewport.scale(Crafty.viewport._scale * (<span class="hljs-number">1</span> + evt.direction * <span class="hljs-number">0.1</span>));
});</code></pre></span></div><div class="example"><h4>例子</h4><span class="markdown"><p>

交互式的，像地图一样缩放视口（摄像机）以响应鼠标滚动事件。 </p>
<pre><code class="hljs "><span class="hljs-comment">// sign public void zoomTowards(Number amt, Number posX, Number posY, Number time[, String|function easingFn])</span>
<span class="hljs-comment">// param Number amt - amount to zoom in on the target by (eg. `2`, `4`, `0.5`)</span>
<span class="hljs-comment">// param Number posX - the x coordinate to zoom towards</span>
<span class="hljs-comment">// param Number posY - the y coordinate to zoom towards</span>
<span class="hljs-comment">// param Number time - the duration in ms of the entire zoom operation</span>
<span class="hljs-comment">// param easingFn - A string or custom function specifying an easing.</span>
<span class="hljs-comment">//                   (Defaults to linear behavior.)</span>
<span class="hljs-comment">//                   See `Crafty.easing` for more information.</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// Zooms the camera towards a given point, preserving the current center.</span>
<span class="hljs-comment">// `amt &gt; 1` will bring the camera closer to the subject,</span>
<span class="hljs-comment">// `amt &lt; 1` will bring it farther away,</span>
<span class="hljs-comment">// `amt = 0` will reset to the default zoom level.</span>
<span class="hljs-comment">// Zooming is multiplicative. To reset the zoom amount, pass `0`.</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// &lt;example&gt;</span>
<span class="hljs-comment">// // Make the entities appear twice as large by zooming in towards (100,100) over the duration of 3 seconds using linear easing behavior</span>
<span class="hljs-comment">// zoomTowards(2, 100, 100, 3000);</span>
<span class="hljs-comment">// &lt;/example&gt;</span>
<span class="hljs-comment">//</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">zoomTowards</span> (<span class="hljs-params">amt, posX, posY, time, easingFn</span>) </span>{
    <span class="hljs-keyword">var</span> scale = Crafty.viewport._scale,
        <span class="hljs-comment">// current viewport center</span>
        centX = -Crafty.viewport._x + Crafty.viewport._width / <span class="hljs-number">2</span> / scale,
        centY = -Crafty.viewport._y + Crafty.viewport._height / <span class="hljs-number">2</span> / scale,
        <span class="hljs-comment">// direction vector from viewport center to position</span>
        deltaX = posX - centX,
        deltaY = posY - centY;
    <span class="hljs-keyword">var</span> f = amt - <span class="hljs-number">1</span>;

    Crafty.viewport.zoom(amt, centX + deltaX * f, centY + deltaY * f, time, easingFn);
}

<span class="hljs-comment">// don't restrict panning of viewport in any way</span>
Crafty.viewport.clampToEntities = <span class="hljs-literal">false</span>;

<span class="hljs-comment">// enable panning of viewport by dragging the mouse</span>
Crafty.viewport.mouselook(<span class="hljs-literal">true</span>);

<span class="hljs-comment">// enable interactive map-like zooming by scrolling the mouse</span>
Crafty.bind(<span class="hljs-string">"MouseWheelScroll"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">evt</span>) </span>{
    <span class="hljs-keyword">var</span> pos = Crafty.domHelper.translate(evt.clientX, evt.clientY);
    zoomTowards(<span class="hljs-number">1</span> + evt.direction/<span class="hljs-number">10</span>, pos.x, pos.y, <span class="hljs-number">5</span>);
});</code></pre></span></div></div></div></div></div>
			
		</div>
	</div>
	<div class="clearer"></div>
	<div id="footer">
		<div id="contact">
			<a href="https://groups.google.com/forum/#!forum/craftyjs">
				<img src="../images/google.png">
				google groups</a>
			<a href="mailto:starwed@gmail.com">
				<img src="../images/email.png">
				starwed@gmail.com</a>
			<a href="https://github.com/craftyjs/Crafty">
				<img src="../images/github.png">
				github</a>
		</div>
		<ul>
			<li><a href="../">首页</a></li>
				<li><a href="../getting-started/">快速开始</a></li>
				<li><a href="../documentation/">文档</a></li>
				<li><a href="../api/">API</a></li>
				<li><a href="../components/">组件</a></li>
				<li class="emph"><a href="../#install">下载</a></li>
		</ul>
		<p>
			© Crafty 2010-2015. Crafty is distributed under the <a href="http://en.wikipedia.org/wiki/MIT_License">MIT License</a>.
		</p>
	</div>
</div>


</body></html>